/* 
 * File:   FileInfo.cpp
 * Author: Dann
 * 
 * Created on 9 de junio de 2013, 08:19 PM
 */

#include "fileinfo.h"

FileInfo::FileInfo(const std::string parentFolder, const std::string name) {
    std::string path = parentFolder;
    path.append("/");
    path.append(name);
    stat(path.c_str(), &st_info);
    this->name = name;
}

FileInfo::FileInfo(const FileInfo& orig) {
}

FileInfo::~FileInfo() {
}

std::string FileInfo::getPermissions() {
    std::string response = S_ISDIR(st_info.st_mode) ? "d" : "-";
    response.append((st_info.st_mode & S_IRUSR) ? "r" : "-");
    response.append((st_info.st_mode & S_IWUSR) ? "w" : "-");
    response.append((st_info.st_mode & S_IXUSR) ? "x" : "-");
    response.append((st_info.st_mode & S_IRGRP) ? "r" : "-");
    response.append((st_info.st_mode & S_IWGRP) ? "w" : "-");
    response.append((st_info.st_mode & S_IXGRP) ? "x" : "-");
    response.append((st_info.st_mode & S_IROTH) ? "r" : "-");
    response.append((st_info.st_mode & S_IWOTH) ? "w" : "-");
    response.append((st_info.st_mode & S_IXOTH) ? "x" : "-");
    return response;
}

std::string FileInfo::getOwner() {
    struct passwd *pw = getpwuid(st_info.st_uid);
    std::string owner(pw->pw_name);
    return owner;
}

std::string FileInfo::getGroup() {
    struct group *gr = getgrgid(st_info.st_gid);
    std::string group(gr->gr_name);
    return group;
}

long long FileInfo::getSize() {
    return st_info.st_size;
}

std::string FileInfo::getLastModificationTime() {
    std::string date(ctime(&st_info.st_mtime));
    return date.substr(0, date.length() - 1);
}

std::string FileInfo::getLastAccessTime() {
    std::string date(ctime(&st_info.st_atime));
    return date.substr(0, date.length() - 1);
}

std::string FileInfo::getName() {
    return name;
}
